import router from './route.js'
import store from './store'
import { getCookie } from '@/utils/cookie.js'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'

//白名单
const whiteList=['/login']

router.beforeEach((to,from,next)=>{
   NProgress.start()
  let session_id=getCookie('session_id');
  if(session_id){
      if(to.path=='/login'){
          next({ path: '/' })
      }else{
          //如果有cookie且跳转到内部页面
          store.commit('setLogin',{
              username:getCookie('username'),
              session_id:getCookie('session_id'),
              user_id:getCookie('user_id'),
              role_id:getCookie('role_id'),
              is_grid:getCookie('is_grid')
          })
          if(to.path=='/'){
             next('/main');
          }
          next();
      }
  }else{
     if(whiteList.indexOf(to.path)!=-1){
        next();
     }else{
        next("/login");
        NProgress.done()
     }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})
